#!/bin/bash

if [[ "$1" == "run" ]]; then
	while :; do
		status=$(nmcli d | grep ethernet)
		status=$(echo $status | cut -d ' ' -f 3)
		
		case "$status" in
		"connecting" )
			running=$(ps -C dhclient | wc -l)
			if [[ $running == "1" ]]; then
				break
			fi
			;;
		"unavailable" )
			;;
		* )
			exit
			;;
		esac
		
		sleep 10
	done
	
	iface=$(nmcli c | grep ethernet | cut -d ' ' -f 1)
	nmcli c up $iface
else
	exec 0>&-
	exec 1>&-
	exec 2>&-
	
	$0 run &
fi
